var editor;
var cpAlertTimer = [];

/*
 * Cookie相关
 */
function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			var endstr = document.cookie.indexOf(";", j);
			if (endstr == -1) {
				endstr = document.cookie.length
			}
			return document.cookie.substring(j, endstr);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break
	}
	return ""
}

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + value + ((expires) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "")
}

/*
 * 提示框
 */
function cpAlert(cpMsg) {
	var status = typeof arguments[1] != 'undefined' ? arguments[1] : 1;
	var msgTipBox = $("#msgTip");
	if (status == -1) {
		msgTipBox.removeClass().addClass('error');
	} else if (status == 0) {
		msgTipBox.removeClass().addClass('worning');
	} else if (status == 1) {
		cpMsg = "<span class='fa fa-check fa-fw' style='color:#6EB43A;'></span>" + cpMsg;
		msgTipBox.removeClass().addClass('success');
	}
	clearTimeout(cpAlertTimer);
	msgTipBox.stop(true, true).html(cpMsg).show().animate({ height: "36px", opacity: 0.8 }, 500, function () {
		cpAlertTimer = setTimeout(function () {
			msgTipBox.animate({ height: "0px" }, 200, function () {
				$(this).hide();
			})
		}, 1200)
	});
}

/*
 * 左边栏菜单加载
 */
function loadLeftMenu(menuId) {
	var dataobj = $(subMenu[menuId]);
	var findCurrent = false;
	dataobj.find("li").each(function () {
		if (window.location.href.indexOf($(this).find('a').attr('href')) != -1) {
			$(this).addClass('cur');
			findCurrent = true;
		}
	});
	if (findCurrent == false) {
		var tryUrl = window.location.href.replace(new RegExp('&a=.*?$'), '');
		tryUrl += '&a=index';
		dataobj.find("li").each(function () {
			if (tryUrl.indexOf($(this).find('a').attr('href')) != -1) {
				$(this).addClass('cur');
			}
		});
	};
	$(".left-menu-list").html(dataobj);
};

/*
 * Ajax 提交表单
 */
function AjaxPost(formObj, btnObj) {
	var btnObjTxt = btnObj.val();
	var formAction = formObj.attr("action");
	var formData = formObj.serialize();
	formData += "&" + btnObj.attr("name") + "=" + btnObj.val();
	btnObj.attr("disabled",true).val("正在提交");
	$.ajax({
		type: "POST",
		url: formAction ? formAction : window.location.href,
		data: formData,
		dataType: "json",
		success: function (data) {
			if (data.status == 1) {
				cpAlert(data.msg, data.status);
				if (typeof data.redirect != 'undefined') {
					if(typeof $.pjax!='undefined'){
						$.pjax({url: data.redirect, container: '.rightframe', fragment:'body', timeout:8000 });
					}else{
						window.location.href = data.redirect;
					}
				}
			} else {
				cpAlert(data.errMsg, data.status);
				if (typeof data.redirect != 'undefined') {
					if(typeof $.pjax!='undefined'){
						$.pjax({url: data.redirect, container: '.rightframe', fragment:'body', timeout:8000 });
					}else{
						window.location.href = data.redirect;
					}
				}
			}
			btnObj.attr("disabled",false).val(btnObjTxt);
		},
		error: function (data) {
			cpAlert(data.responseText.replace(/<\/?[^>]*>/g, ''), 0);
			btnObj.attr("disabled",false).val(btnObjTxt);
		}
	});
}

function toggle_jnarrow() {
	var is_jnarrow = getCookie('jnarrow');
	is_jnarrow = is_jnarrow ^ 1;
	setCookie('jnarrow', is_jnarrow, 0, '/');
	if (is_jnarrow == 1) {
		$(".header,.left-menu,.cp-wrap").addClass("jnarrow");
	} else {
		$(".header,.left-menu,.cp-wrap").removeClass("jnarrow");
	}
}

function loadEditor(){
	if(typeof UM !== 'undefined'){
		$("textarea[_type=editor],script[_type=editor]").each(function(){
			var name = $(this).attr("id");
			var editorOption = {
				toolbar: window.UMEDITOR_CONFIG.toolbarYuMingCMS
			}
			if(editor){
				editor.destroy();
			}
			editor = UM.getEditor(name, editorOption);
		})
	}
}

function pageInit(){
	$(".AjaxPost [type=submit]").on('click', function(){
		AjaxPost($(this).closest('form'), $(this));
		return false;
	});
	$('.cp-form select').select2({minimumResultsForSearch: -1});
	$(".tooltip").remove();
	$('.rightframe div,.rightframe a').tooltip({gravity: $.fn.tooltip.autoNS});
	if ($('.crumb').length > 0 && $(".left-menu-list").find('.cur a').length>0) {
		crumb = $(".left-menu-list").find('.cur a').prop("outerHTML") + ' ' + $('.crumb').html();
		$('.crumb').html(crumb);
	}
}
